package com.ft.controller;

import com.alibaba.fastjson.JSONObject;
import com.ft.config.properties.WeiXinPayProperties;
import com.ft.domain.WeiXinPayToUniapp;
import com.ft.service.impl.WxPayServiceImpl;
import com.ft.utils.Code;
import com.ft.utils.MD5Utils;
import com.ft.utils.Result;
import com.ft.utils.WxPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ft
 * @Date: 2022/10/08/14:24
 * @Description: 微信支付接口
 */
@RestController
@RequestMapping("/wxpay")
@CrossOrigin
public class WeiXinPayController {
    @Autowired
    private WeiXinPayProperties weiXinPay;
    @Autowired
    private WxPayServiceImpl wxsl;
    @GetMapping("/{openid}")
    public Result pay(@PathVariable String openid) throws Exception {
        Map<String,String> js = new HashMap<String,String>();
        String ip = "127.0.0.1";
        js.put("appid", weiXinPay.getAppid());
        js.put("mch_id", weiXinPay.getMchid());
        //随机字符串
        js.put("nonce_str", MD5Utils.randomStr());
        /**
         * 业务数据
         */
        //订单名称
        js.put("body", "测试支付1");
        //订单号
        js.put("out_trade_no", "2015080238124349");
        //订单价格
        js.put("total_fee","1");
        //付款ip
        js.put("spbill_create_ip", ip);
        //回调地址，小程序可以不用写
        js.put("notify_url", "https://www.baidu.com");
        js.put("trade_type", weiXinPay.getType());
        js.put("openid",openid);
        //签名
        String postStr = WxPayUtils.Md5Sgin(js,weiXinPay.getAppsecret());
        js.put("sign",postStr);
        String str = WxPayUtils.sendPost(js);
        WeiXinPayToUniapp weiXinPayToUniapp = wxsl.wxPay(weiXinPay.getGateway(), str);
        return new Result(Code.GET_OK, weiXinPayToUniapp,"请求成功");
    }
}
